package com.example.edu.controller.frontController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.edu.entity.Chapter;
import com.example.edu.entity.Course;
import com.example.edu.entity.course.EduChapter;
import com.example.edu.entity.frontvo.CourseQueryVo;
import com.example.edu.entity.frontvo.CourseWebVo;
import com.example.edu.service.ChapterService;
import com.example.edu.service.CourseService;
import com.example.utils.Result;
import com.example.utils.order.OrderCourse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/edu")
@CrossOrigin
public class CourseFrontController {

    @Autowired
    CourseService courseService;

    @Autowired
    ChapterService chapterService;

    @PostMapping("/getFrontCourse/{page}/{limit}")
    public Result getFrontCourse(@PathVariable Long page, @PathVariable Long limit
    , @RequestBody(required = false) CourseQueryVo courseQueryVo) {
        Page<Course> coursePage = new Page<Course>(page,limit);
        Map<String,Object> map = courseService.getFrontCourse(coursePage,courseQueryVo);
        return Result.ok().data(map);
    }

    @GetMapping("/getFrontCourse/{courseId}")
    public Result getFrontCourseInfo(@PathVariable String courseId) {
        System.out.println("haha");
        CourseWebVo courseWebVo = courseService.getFrontCourseInfoById(courseId);
        List<EduChapter> allChapterVideo = chapterService.getAllChapterVideo(courseId);
        System.out.println(courseWebVo);
        return Result.ok().data("courseWebVo", courseWebVo).data("allChapterVideo",allChapterVideo);
    }
    
    @GetMapping("/getOrderCourse/{courseId}")
    public OrderCourse getOrderCourse(@PathVariable String courseId) {
        System.out.println("haha");
        CourseWebVo courseWebVo = courseService.getFrontCourseInfoById(courseId);
        OrderCourse orderCourse = new OrderCourse();
        BeanUtils.copyProperties(courseWebVo, orderCourse);
        return orderCourse;
    }
}
